package com.zut.chat.controller;

import com.zut.chat.service.GiftService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼物控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/gifts")
public class GiftController {

    @Autowired
    private GiftService giftService;

    /**
     * 获取所有礼物列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getGiftList() {
        try {
            List<GiftService.Gift> gifts = giftService.getAllGifts();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取礼物列表成功");
            response.put("data", gifts);
            response.put("total", gifts.size());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取礼物列表失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取礼物列表失败：" + e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 根据名称获取礼物信息
     */
    @GetMapping("/{name}")
    public ResponseEntity<Map<String, Object>> getGiftByName(@PathVariable String name) {
        try {
            GiftService.Gift gift = giftService.getGiftByName(name);
            
            Map<String, Object> response = new HashMap<>();
            if (gift != null) {
                response.put("success", true);
                response.put("message", "获取礼物信息成功");
                response.put("data", gift);
            } else {
                response.put("success", false);
                response.put("message", "礼物不存在");
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取礼物信息失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取礼物信息失败：" + e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
} 